#!/bin/bash
#Time
log_time=`date +[%Y-%m-%d]%H:%M:%S`

###manual_properties###
tomcat_basehome=/usr/local/tomcat/apache-tomcat-8.5.20
tomcat_port=80
shell_environment=/bin/bash
war_Dir=/webapps/service/target
war_Name=xing-service.war
###manual_properties###


echo "**********************************  ${log_time} *************************************"

#build check funcation
echo "check tomcat status..."
check_tomcat_status(){
      if [ `ps auxwwww|grep apache-tomcat-8.5.20|grep -v grep|wc -l` -gt 0 ]
      then
      for pid in `ps auxwww|grep apache-tomcat-8.5.20|grep -v grep|tr -s ' '|cut -d ' ' -f2`
      do
      kill -9 $pid 2>&1 > /dev/null
      done
      fi
}

#check tomcat status invoke function
check_tomcat_status


#transfer  application package
deploy_Loaction=${tomcat_basehome}/webapps/
war_Dir_Data=`ls ${war_Dir}`
echo "--------------  begin  transfer  war package to tomcat webapps -------------------"

if [ -z $war_Dir ];then
     echo "Folder ${war_Dir} is empty.please check war package in this folder!"
     exit 1
else
     echo "Find ${war_Dir} exist war package ${war_Name}"
    # echo "deleteing old  package ${war_Name} in ${war_Dir}"
    # rm ${war_Dir}/${war_Name}
     echo "deleteing old  package ${war_Name} in ${deploy_Loaction}"
     rm ${deploy_Loaction}${war_Name}
     echo "start  transfer ${war_Name} to ${deploy_Loaction}"
     cp ${war_Dir}/${war_Name}  ${deploy_Loaction}
     sleep 3
fi
echo "--------------  transfer  war package to tomcat webapps  end -------------------"
#reboot tomcat
echo " >>>>>>>  rebooting  tomcat begin <<<<<<<<"
${shell_environment} ${tomcat_basehome}/bin/startup.sh
echo " >>>>>>>  rebooting  tomcat end <<<<<<<<"
echo "the log you can read in canalina.out"
echo "************************ deploy war package into container Successlly  **********************************"